const app = getApp()
const commissionApi = require('../../../utils/commissionApi.js')

Page({
  data: {
    list: [],
    page: 1,
    pageSize: 10,
    total: 0,
    loading: false,
    finished: false,
    userId: null
  },

  onLoad() {
    const userId = app.globalData.userInfo.user_id  // 假设你登录后有这个
    this.setData({ userId }, () => {
      this.loadData()
    })
  },

  async loadData() {
    if (this.data.finished || this.data.loading) return

    this.setData({ loading: true })

    try {
      const res = await commissionApi.getCommissionList({
        user_id: this.data.userId,
        page: this.data.page,
        page_size: this.data.pageSize
      })

      if (res.total !== undefined && res.list) {
        const newList = this.data.list.concat(res.list)
        const finished = newList.length >= res.total
        this.setData({
          list: newList,
          total: res.total,
          finished,
          totalAmount:res.total_amount,
          page: this.data.page + 1
        })
      }
    } catch (err) {
      wx.showToast({ title: '加载失败', icon: 'none' })
    } finally {
      this.setData({ loading: false })
    }
  },

  onReachBottom() {
    this.loadData()
  }
})
